<!DOCTYPE html>
<html lang="en">
<head>
    <title>部位列表</title>
    <%- include('../../_global/baseLibs') %>
</head>
<body>

<section class="page-wrap page-health page-body-disease">
    <!-- header start -->
    <header id="header" class="header-box grey fixed header-box">
        <div class="header-content">
            <div class="left-tool">
                <a class="back" href="javascript:void(0);"><i class="iconfont icon-back"></i></a>
            </div>
            <div class="title">部位列表</div>
        </div>
    </header>
    <!-- header end -->
    <section class="body-disease-box">
        <!--left start-->
        <aside class="disease-left">
            <ul class="body-left-list" id="disease-left">

            </ul>
        </aside>
        <!--left end-->

        <!--right start-->
        <aside class="disease-right" id="disease-right">

        </aside>
        <!--right end-->

    </section>

</section>

<script type="text/javascript" src="/static/lib/util.js"></script>
<script type="text/html" id="left_item">
    <li><a href="javascript:void(0);"></a></li>
</script>
<script type="text/html" id="right_div">
    <div class="tab"><ul class="body-right-list"></ul></div>
</script>
<script type="text/html" id="right_item">
    <li><a></a></li>
</script>

<script>
    var page_partList = (function($, page_partList) {
        var uri_service_getBodyList = '/api/gzd/disease/getBodyList';

        function rightItemClickHandler(){
            var part_name = $(this).text();
            var item="part_name="+part_name;
            $.pageSwitch.goToUrl("diseaseTest","diseaseList",item);
//        jump(url);
        }

        function getList(gender){
            $.QuickRemote.AjaxJson({
                funcName: uri_service_getBodyList
                , params: {gender:gender}
                , callback: function (result) {
                    page_partList.createItems(result);
                    var id = $("body").data("body_id");
                    $("#disease-left").find("li[id='" + id + "']").trigger('click');
                }
            });
        }

        /*创建两级目录*/
        function createItems(result){
            var ary = page_partList.createHire(result);
            $.each(ary, function(){
                var item = page_partList.createLeftItem(this);
                $("#disease-left").append(item);
                var div = page_partList.createRigth(this['children']);
                $("#disease-right").append(div);

            })
        }
        /*创建层级结构*/
        function createHire(result){
            var bodys = [];
            var pre_body_parts = '';
            var pre_id = '';
            var pre_chileren = [];
            $.each(result, function(index, item){
                var id = item['id'];
                var body_parts = item['body_parts'];
                var part_name = item['part_name'];

                if(index == 0){
                    pre_id = id;
                    pre_body_parts = body_parts;
                    pre_chileren.push(part_name);
                }else{
                    if(body_parts != pre_body_parts){
                        bodys.push({
                            id: pre_id
                            ,body_parts: pre_body_parts
                            ,children: pre_chileren
                        });
                        pre_id = id;
                        pre_body_parts = body_parts;
                        pre_chileren = [];
                    }else{
                        pre_chileren.push(part_name);
                    }
                }
            });
            bodys.push({
                id: pre_id
                ,body_parts: pre_body_parts
                ,children: pre_chileren
            });
            return bodys;
        }

        function createLeftItem(data){
            var item = $($("#left_item").html());
            item.find("a").html(data['body_parts']);
            item.attr("id", data['id']);
            return item;
        }

        function createRigth(ary){
            var div = $($("#right_div").html());
            $.each(ary, function(){
                var item = $($("#right_item").html());
                item.find("a").html(this);
                div.find(".body-right-list").append(item);
            })
            return div;
        }

        var cfg_def = {
            rightItemClickHandler: rightItemClickHandler,
            getList: getList,
            createItems: createItems,
            createHire: createHire,
            createLeftItem: createLeftItem,
            createRigth: createRigth,
        }

        page_partList.init = function(cfg){
            $.extend(page_partList, cfg_def, cfg);
            $(".body-left-list").on('click','li',function(){
                var index = $(this).index();
                $(this).addClass('active').siblings().removeClass('active');
                $(".disease-right").find('.tab').removeClass('active').eq(index).addClass('active');
            });

            var source = $.QuickUrlUtils.getRequest('source');
            var reqs = source.split('_');
            var body_id = reqs[1];
            $("body").data("body_id", body_id);
            if(reqs[0] == "men"){
                page_partList.getList('1')//男
            }else{
                page_partList.getList('2')//女
            }

            $("#disease-right").on("click", ".body-right-list li", page_partList.rightItemClickHandler);

        }

        return page_partList;
    })(jQuery, {});




</script>
</body>

</html>